﻿#include "GameRenderer.h"
#include <graphics.h>
#include <tchar.h>

void GameRenderer::render() {
    cleardevice();
    drawBoard();
    drawPieces();
    drawTipText();
    FlushBatchDraw();
}

void GameRenderer::drawBoard() const {
    // 绘制横线
    line(0, gameState.Height / 3, gameState.Width, gameState.Height / 3);
    line(0, gameState.Height * 2 / 3, gameState.Width, gameState.Height * 2 / 3);
    // 绘制竖线
    line(gameState.Width / 3, 0, gameState.Width / 3, gameState.Height);
    line(gameState.Width * 2 / 3, 0, gameState.Width * 2 / 3, gameState.Height);
}

void GameRenderer::drawPieces() {
    const auto& boardData = gameState.boardData;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            char c = boardData[i][j];
            // 计算每个格子的中心点
            int centerX = j * gameState.Width / 3 + gameState.Width / 6;
            int centerY = i * gameState.Height / 3 + gameState.Height / 6;
            // 计算绘制尺寸，取宽高的较小值的10%作为绘制尺寸
            int size = min(gameState.Width, gameState.Height) / 10;

            if (c == 'O') {
                // 绘制圆形
                setlinecolor(RED);
                circle(centerX, centerY, size);
            }
            else if (c == 'X') {
                // 绘制十字
                setlinecolor(BLUE);
                line(centerX - size, centerY - size, centerX + size, centerY + size);
                line(centerX + size, centerY - size, centerX - size, centerY + size);
            }
        }
    }
}

void GameRenderer::drawTipText() {
    TCHAR buf[64];
    // 提示信息
    _stprintf(buf, _T("当前棋子：%c    （点击任意空格落子）"), gameState.getCurrentPiece());
    settextcolor(RGB(225, 175, 45)); // 设置文本颜色
    settextstyle(20, 0, _T("微软雅黑")); // 设置字体和大小
    outtextxy(10, 10, buf);  // 显示文本
}
